package com.indusfo.spc.service.impl;

import com.indusfo.spc.client.mapper.ClientAnoMapper;
import com.indusfo.spc.client.pojo.ClientAno;
import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.client.mapper.ClientAnoDetailMapper;
import com.indusfo.spc.client.pojo.ClientAnoDetail;
import com.indusfo.spc.service.WebClientAnoDetailService;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;


@Service
public class WebClientAnoDetailServiceImpl implements WebClientAnoDetailService {

    private  static  final Logger logger = LoggerFactory.getLogger(WebClientAnoDetailServiceImpl.class);

    @Resource
    private ClientAnoDetailMapper clientAnoDetailMapper;

    @Resource
    private ClientAnoMapper clientAnoMapper;

    /**
     * 删除 停用 启用
     * @param clientAnoDetailIds
     * @param dataState
     * @return
     */
    @Override
    public JSONObject definedClientAnoDetail(Long[] clientAnoDetailIds, Integer dataState) {
        try{
            //判断传入的参数是否为null
            if(dataState == null) {
                //为null抛出异常
                throw new ParamsErrorException("数据状态不能为空");
            }else if(dataState != 1 && dataState != 2 && dataState != 3) {
                throw new ParamsErrorException("数据状态错误");
            }
            if(clientAnoDetailIds == null || clientAnoDetailIds.length == 0) {
                throw new ParamsErrorException("请选择异常单明细");
            }
            //调用删除的接口
            int row = clientAnoDetailMapper.definedClientAnoDetail(clientAnoDetailIds,dataState);
            //声明msg字符串，用来存抛出的异常
            String msg = "";
            if (row == 0) {
                // 判断传入的数据状态参数dataState,返回相应信息
                switch (dataState) {
                    case 1:
                        throw new ModifyFailedException("启用失败");
                    case 2:
                        throw new ModifyFailedException("删除失败");
                    case 3:
                        throw new ModifyFailedException("停用失败");
                    default:
                        break;
                }
            } else {
                switch (dataState) {
                    case 1:
                        msg = "启用成功";break;
                    case 2:
                        msg = "删除成功";break;
                    case 3:
                        msg = "停用成功";break;
                    default:
                        break;
                }
            }
//            judgeAnoState(clientAnoDetail);
            return JSONObject.oK(msg);
        }catch(
                GlobalException e){//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 新增
     * @param clientAnoDetail
     * @return
     */
    @Override
    public JSONObject insertClientAnoDetail(ClientAnoDetail clientAnoDetail) {
        try{
            //判断传入的参数是否为null
            if(clientAnoDetail.getClientAnoDetailId()!=null) {
                //为null则抛出异常
                throw new ParamsErrorException("新增异常单明细时不能有id");
            }
            checkParam(clientAnoDetail);
            int row = clientAnoDetailMapper.insertSelective(clientAnoDetail);
            if(row == 0) {
                throw new ModifyFailedException("新增异常单明细失败");
            }
            judgeAnoState(clientAnoDetail);
            return JSONObject.oK("新增成功");
        }catch(
                GlobalException e){//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    /**
     * 编辑
     * @param clientAnoDetail
     * @return
     */
    @Override
    public JSONObject updateClientAnoDetail(ClientAnoDetail clientAnoDetail) {
        try {
            //判断传入的参数是否为null
            if(clientAnoDetail.getClientAnoDetailId()==null) {
                //为null则抛出异常
                throw new ParamsErrorException("修改异常单明细id不能为空");
            }
            checkParam(clientAnoDetail);
            //调用修改的接口
            int row = clientAnoDetailMapper.updateByPrimaryKeySelective(clientAnoDetail);
            if(row == 0) {
                throw new ModifyFailedException("编辑失败");
            }
            judgeAnoState(clientAnoDetail);
            return JSONObject.oK("编辑成功");
        }catch (GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    public void judgeAnoState(ClientAnoDetail clientAnoDetail) {
        //调用修改的接口
        int row = clientAnoDetailMapper.judgeAnoState(clientAnoDetail);
        ClientAno clientAno = clientAnoMapper.selectByPrimaryKey(clientAnoDetail.getClientAnoId());
        if(row == 0 && StringUtils.isEmpty(clientAno.getEndDate())) {
            clientAnoMapper.updateAnoState(clientAnoDetail.getClientAnoId() , 3 ,null ,"1");
        }else if(row == 0 && !StringUtils.isEmpty(clientAno.getEndDate())){
            clientAnoMapper.updateAnoState(clientAnoDetail.getClientAnoId() , 3,null ,null);
        }else if(row != 0 && StringUtils.isEmpty(clientAno.getStartDate())){
            clientAnoMapper.updateAnoState(clientAnoDetail.getClientAnoId() , 2,"1" ,null);
        }else if(row != 0 && !StringUtils.isEmpty(clientAno.getStartDate())){
            clientAnoMapper.updateAnoState(clientAnoDetail.getClientAnoId() , 2,null ,null);
        }
    }

    /**
     * 校验参数
     * @param clientAnoDetail
     */
    public void checkParam(ClientAnoDetail clientAnoDetail){
        if(clientAnoDetail.getClientAnoId()==null){
            throw new ParamsErrorException("请选择异常单");
        }
        if(clientAnoDetail.getBadphenoId()==null){
            throw new ParamsErrorException("请选择不良现象");
        }
        if(clientAnoDetail.getBadcauseId()==null){
            throw new ParamsErrorException("请选择不良原因");
        }
        if(clientAnoDetail.getBadreplyId()==null){
            throw new ParamsErrorException("请选择不良对策");
        }
    }
}
